package com.regulareport.web.controller.report;

import com.regulareport.common.annotation.Log;
import com.regulareport.common.core.controller.BaseController;
import com.regulareport.common.core.domain.AjaxResult;
import com.regulareport.common.core.domain.entity.HeadSubIndexMapQuery;
import com.regulareport.common.core.domain.entity.NotMapListQuery;
import com.regulareport.common.core.page.TableDataInfo;
import com.regulareport.common.enums.BusinessType;
import com.regulareport.form.domain.*;
import com.regulareport.form.service.IHeadIndexMapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/reportMap")
public class HeadSubIndexMapController extends BaseController {
    @Autowired
    private IHeadIndexMapService headIndexMapService;

    /**
     * 查询Sheet列表
     */
    @PreAuthorize("@ss.hasPermi('report:reportMap:mapList')")
    @PostMapping("/mapEditionList")
    public TableDataInfo mapEditionList(@RequestBody MapEditionListQuery query) {
        startPage();
        List<MapEditionListVO> mapListVOS = headIndexMapService.mapEditionList(query);
        return getDataTable(mapListVOS);
    }

    /**
     * 查询Map列表
     */
    @PreAuthorize("@ss.hasPermi('report:reportMap:list')")
    @PostMapping("/mapList")
    public TableDataInfo mapList(@RequestBody HeadSubIndexMapQuery headSubIndexMapQuery) {
        startPage();
        List<ReportHeadSubMapVo> reportHeadSubMapVos = headIndexMapService.mapList(headSubIndexMapQuery);
        return getDataTable(reportHeadSubMapVos);
    }

    /**
     * 导入总行表
     */
    @PreAuthorize("@ss.hasPermi('report:reportMap:import')")
    @Log(title = "总行表导入", businessType = BusinessType.IMPORT)
    @PostMapping(value = "/import")
    public AjaxResult edit(MultipartFile file) {
        try {
            headIndexMapService.parse(file);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @PreAuthorize("@ss.hasPermi('report:reportMap:notMapList')")
    @PostMapping("/notMapList")
    public AjaxResult notMapList(@RequestBody NotMapListQuery notMapListQuery) {
        try {
            return AjaxResult.success(headIndexMapService.notMapList(Long.valueOf(notMapListQuery.getMapEditionId())));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @PreAuthorize("@ss.hasPermi('report:reportMap:edit')")
    @PostMapping("/edit")
    @Log(title = "编辑映射", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@RequestBody HeadSubIndexEdit headSubIndexMap) {
        try {
            headIndexMapService.edit(headSubIndexMap);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @PreAuthorize("@ss.hasPermi('report:reportMap:enable')")
    @PostMapping("/enable")
    @Log(title = "启停关系", businessType = BusinessType.UPDATE)
    public AjaxResult enable(@RequestBody NotMapListQuery notMapListQuery) {
        try {
            headIndexMapService.enable(notMapListQuery);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @PreAuthorize("@ss.hasPermi('report:reportMap:delete')")
    @PostMapping("/delete")
    @Log(title = "删除映射关系", businessType = BusinessType.DELETE)
    public AjaxResult delete(@RequestBody NotMapListQuery notMapListQuery) {
        try {
            headIndexMapService.delete(notMapListQuery);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
}
